var app =getApp();
var httpUtils = require('../../utils/httpUtils.js');
var utils = require('../../utils/util.js');
Page({
  data:{
    icon:'',
    name:'暂无姓名',
    idCard: '',
    tel: '暂无电话',
    usableAmount: '0.00',
    usableAmount: '0.00',
    hiddenModalInput:true,
    nameInput:'',
    idCardInput:''
  },
  onLoad: function () {
    var _this = this ;
    var url = app.globalData.interfaceUrl + '/worker.do?method=workerAccountInfo' ;
    console.log(url);
      httpUtils.post(url,{},function(res){
        console.log(JSON.stringify(res));
        var personalData = res.data.data;
        _this.setData({
          icon:personalData.icon,
          idCard: personalData.idCard,
          tel: personalData.tel,
          usableAmount: personalData.usableAmount,
          waitReceiptAmount: personalData.waitReceiptAmount,
          name: personalData.name,
          nameInput: personalData.name,
          idCardInput: personalData.idCardMw
        })
      });
  },
  gotoEditPersonalInfo: function () {
    this.setData({
      hiddenModalInput:false 
    });
  },
  gotoAvailableBalance: function () {
    wx.navigateTo({
      url: '../availableBalance/availableBalance'
    })
  },
  gotoAccountPayable: function () {
    wx.navigateTo({
      url: '../accountPayable/accountPayable'
    })
  },
  gotoQA: function () {
    wx.navigateTo({
      url: '../qa/qa'
    })
  },
  gotoIntroduce: function () {
    wx.navigateTo({
      url: '../introduce/introduce'
    })
  },
  cancel: function () {
    this.setData({
      hiddenModalInput: true
    })
  },
  confirm: function () {
    console.log('这是啥');
    console.log(wx.getStorageSync('userInfo'));
    var name = this.data.nameInput;
    var idCard = this.data.idCardInput;


    if (!utils.isIdCardNo(idCard)) {
      wx.showToast({
        title: '请设置正确的身份证号码.',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    //吧框隐藏掉
    this.setData({
      hiddenModalInput: true
    });

    var _this = this;
    var url = app.globalData.interfaceUrl + '/worker.do?method=updateWorkerInfo';
    console.log(url);
    httpUtils.post(url, {
      'workerName': name,
      'idCard': idCard
    }, function (res) {
      var resp = res.data;
      var userInfo = wx.getStorageSync('userInfo');
      userInfo.workerName = name;
      userInfo.idCard = idCard;
      wx.setStorageSync('userInfo', userInfo);
      console.log(JSON.stringify(userInfo));
      wx.switchTab({
        url: '../personal/personal',
        success: function (e) {
          var page = getCurrentPages().pop();
          if (!utils.isNull(page)) {
            page.onLoad();
          }
        }
      });
      wx.showToast({
        title: '操作成功',
        icon: 'succes',
        duration: 1000,
        mask: true
      })
    });
  },
  //获取用户输入的名字
  nameInput: function (e) {
    console.log(e);
    this.setData({
      nameInput: e.detail.value
    })
  },
  //获取用户输入的身份证号
  idCardInput: function (e) {
    console.log(e);
    this.setData({
      idCardInput: e.detail.value
    })
  }
})